/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NM_Servlet;

import NM_ClasesGenerales.Cliente;
import NM_ClasesGenerales.Passenger;
import NM_ClasesGenerales.Solicitud;
import NM_Procesos.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author USER
 */
public class NM_RecuperarPNR extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NM_RecuperarPNR</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NM_RecuperarPNR at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sesion = request.getSession(true);
        RecuperarPNR oRecuperarPNR = new RecuperarPNR();
        Solicitud objSolicitud = new Solicitud();
        int gds = -1;
        String info = "";
        Cliente oUsuario = new Cliente();

        try {

            if (sesion.getAttribute("objusuario") != null) {
                oUsuario = (Cliente) sesion.getAttribute("objusuario");
            }
            if (sesion.getAttribute("xSolicitud") != null) {
                objSolicitud = (Solicitud) sesion.getAttribute("xSolicitud");
            } else {
                gds = Integer.parseInt(request.getParameter("HiddenField2"));
                if (gds == 1) {
                    info = "SABRE";
                    objSolicitud = oRecuperarPNR.Sabre(request.getParameter("textpnr"));//lista
                } else {
                    info = "AMADEUS";
                    objSolicitud = oRecuperarPNR.Sabre(request.getParameter("textpnr"));//lista
                }
            }
            request.setAttribute("xUsuario", oUsuario);

            if (objSolicitud.getObjItinerario().size() > 0 && objSolicitud.getObjPasseger().size() > 0) {
                request.setAttribute("req_textpnr", request.getParameter("textpnr"));
                request.setAttribute("req_gds", info);
                request.setAttribute("xSolicitud", objSolicitud);
                request.setAttribute("listaPassenger", objSolicitud.getObjPasseger());
                request.setAttribute("listaItinerario", objSolicitud.getObjItinerario());
                request.setAttribute("HiddenField1", null);

            } else {
                request.setAttribute("HiddenField1", "Numero de Solicitud no encontrado.");
            }
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/app_sabre/reserva.jsp");

            dispatcher.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(NM_RecuperarPNR.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage() + "//////////////////////////////////////////////");
        }
        /* processRequest(request, response); */
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
